package com.ruoyi.web.controller.hsz;


import com.mybatisflex.core.query.QueryWrapper;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.system.domain.HszArticle;
import com.ruoyi.system.domain.HszClazz;
import com.ruoyi.system.service.IHszArticleService;
import com.ruoyi.system.service.IHszClazzService;
import com.ruoyi.web.controller.hsz.param.HszClazzEditParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

import static com.ruoyi.system.domain.table.HszClazzTableDef.HSZ_CLAZZ;

/**
 * 分类管理
 *
 * @author zhangqingshan
 * @date 2025-08-28
 */
@RestController
@RequestMapping("/hsz/clazz")
@Slf4j
public class ClazzManagerController {

    @Autowired
    private IHszClazzService clazzService;

    @Autowired
    private IHszArticleService articleService;

    /**
     * 编辑
     * 权限：hsz:clazz:edit
     *
     * @param param 参数
     * @return {@link R }
     */
    @RequestMapping("/edit")
    @PreAuthorize("@ss.hasPermi('hsz:clazz:edit')")
    @Transactional(rollbackFor = Exception.class)
    public R edit(@RequestBody @Validated HszClazzEditParam param) {
        HszClazz hszClazz = param.toDO();
        if (Objects.nonNull(hszClazz.getClazzId()) && Objects.isNull(clazzService.getById(hszClazz.getClazzId()))) {
            return R.fail("分类不存在,无法编辑");
        }
        if (Objects.nonNull(hszClazz.getPid()) && Objects.isNull(clazzService.getById(hszClazz.getPid()))) {
            return R.fail("上级分类不存在,无法关联");
        }
        return clazzService.saveOrUpdate(hszClazz) ? R.ok() : R.fail();
    }


    /**
     * 删除
     * 权限：hsz:clazz:del
     *
     * @param clazzId 克拉兹 ID
     * @return {@link R }
     */
    @DeleteMapping("/delete/{clazzId}")
    @PreAuthorize("@ss.hasPermi('hsz:clazz:del')")
    @Transactional(rollbackFor = Exception.class)
    public R delete(@PathVariable("clazzId") Long clazzId) {
        return clazzService.removeById(clazzId) ? R.ok() : R.fail();
    }

    /**
     * 详情
     *
     * @param clazzId 克拉兹 ID
     * @return {@link R }<{@link HszClazz }>
     */
    @Anonymous
    @GetMapping("/detail/{clazzId}")
    public R<HszClazz> detail(@PathVariable("clazzId") Long clazzId) {
        return R.ok(clazzService.getById(clazzId));
    }

    /**
     * 树形结构
     *
     * @param clazzId 分类id，传0 可查询全部
     * @return {@link R }<{@link List }<{@link HszClazz }>>
     */
    @Anonymous
    @GetMapping("/tree")
    public R<List<HszClazz>> tree(@RequestParam(value = "clazzId", required = false) Long clazzId) {
        List<HszClazz> list = clazzService.list(QueryWrapper.create().from(HszClazz.class)
                .where(HSZ_CLAZZ.CLAZZ_ID.eq(clazzId).when(Objects.nonNull(clazzId) && 0 != clazzId))
                .isNull("pid", Objects.isNull(clazzId) || 0 == clazzId)
                .orderBy(HSZ_CLAZZ.SORT.asc()));
        clazzService.cycleClazzTree(list);
        return R.ok(list);
    }

    /**
     * 通过文章 ID 获取 Clazz 路径
     *
     * @param articleId 文章 ID
     * @return {@link R }<{@link Object }>
     */
    @Anonymous
    @GetMapping("/getClazzByArticleId/{articleId}")
    public R<Object> getClazzByArticleId(@PathVariable("articleId") Long articleId){
        HszArticle article = articleService.getById(articleId);
        if (Objects.isNull(article)){
            return R.fail("文章不存在");
        }
        HszClazz clazz = clazzService.getById(article.getClazzId());
        if (Objects.isNull(clazz)){
            return R.fail("分类不存在");
        }
        return R.ok(clazzService.path(clazz));
    }

    /**
     * 通过 ID 获取 Clazz 路径
     *
     * @param clazzId 克拉兹 ID
     * @return {@link R }<{@link HszClazz }>
     */
    @Anonymous
    @GetMapping("/getClazzById/{clazzId}")
    public R<HszClazz> getClazzById(@PathVariable("clazzId") Long clazzId){
        HszClazz clazz = clazzService.getById(clazzId);
        if (Objects.isNull(clazz)){
            return R.fail("分类不存在");
        }
        return R.ok(clazzService.path(clazz));
    }
}
